<script setup>
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { getGoodsDetailAPI, getGoodsGroupAPI, getGoodsSkuAPI } from '../../../apis/goods';

defineOptions({
    name: 'shopDetail'
})

const route = useRoute()

let goodsId = ref(route.query.goodsId)
let goodsDetail = ref({})
let goodsGroup = ref([])
let photoList = ref([])  //商品图片列表
let currentPhoto = ref()    //当前图片
let currentPhotoIndex = ref(0)   // 当前图片下标
let groupPhotoList = ref([])
let groupIndex = ref(0)
let skuList = ref([])
let selectedShoes = ref({
    size: 0,
    price: 0,
    inventory: ''
})
let mainLargeRef = ref(null)
let showBlowUp = ref(false)     // 是否展示放大镜

onMounted(() => {
    getGoodsDetail(goodsId.value)
})

// 商品基础详情
const getGoodsDetail = async (id) => {
    let res = await getGoodsDetailAPI(id)
    if (res.code === 200) {
        photoList.value = res.data.goods[0].goods_img.split(',')
        currentPhoto.value = photoList.value[0]
        goodsDetail.value = res.data.goods[0]
        // goodsGroup.value = res.data.goodsGroup
        // groupPhotoList.value = goodsGroup.value.map(item => {
        //     return item.goods_img.split(',')[0]
        // })
        if (goodsGroup.value.length === 0) {
            getGroupGoods()
        }
        getGoodsSku(id)
    }
}

const getGroupGoods = async () => {
    let res = await getGoodsGroupAPI(goodsDetail.value.goods_group)
    if (res.code === 200) {
        goodsGroup.value = res.data
        groupPhotoList.value = goodsGroup.value.map(item => {
            return {
                goods_id: item.goods_id,
                goods_img: item.goods_img.split(',')[0]
            }
        })
    }
}

const getGoodsSku = async (id) => {
    let res = await getGoodsSkuAPI(id)
    if (res.code === 200) {
        skuList.value = res.data
    }
}

// 单击切换图片
const changePhotoAction = (photo, index) => {
    currentPhoto.value = photo
    currentPhotoIndex.value = index
}

// 单击切换配色
const changeGroupAction = (id, index) => {
    groupIndex.value = index
    getGoodsDetail(id)
}

const selectSizeAction = (item) => {
    console.log(item);
    selectedShoes.value.size = item.sku_size
    selectedShoes.value.price = item.sku_price
    selectedShoes.value.inventory = item.sku_inventory
}

const largeMoveAction = (event) => {
    console.log(event.offsetX, event.offsetY);

}


</script>

<template>
    <div class="page">
        <div class="container">
            <div class="temp"></div>
            <div class="main">
                <div class="bread">
                    <div class="bread-back"><el-button size="small" @click="back">返回</el-button></div>
                    <div class="bread-link">
                        <el-breadcrumb class="bread-link">
                            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                            <el-breadcrumb-item>
                                <div><span>{{ goodsDetail?.goods_name }}</span></div>
                            </el-breadcrumb-item>
                        </el-breadcrumb>
                    </div>
                </div>
                <div class="goods">
                    <div class="goods-aside"></div>
                    <div class="goods-photo">
                        <div class="photo-main">
                            <div class="main-large" @mousemove="largeMoveAction" ref="mainLargeRef"><img
                                    :src="currentPhoto" alt=""></div>
                            <div class="main-list">
                                <div :class="{ mainListItem: true, mainListItemActive: currentPhotoIndex === index }"
                                    v-for="(item, index) in photoList" :key="index"
                                    @click="changePhotoAction(item, index)">
                                    <img :src="item" alt="">
                                </div>
                            </div>
                        </div>

                        <div class="photo-type">
                            <div>更多配色</div>
                            <div class="photo-type-list">
                                <div :class="{ photoTypeItem: true, photoTypeItemActive: groupIndex === index }"
                                    v-for="(item, index) in groupPhotoList" :key="index"
                                    @click="changeGroupAction(item.goods_id, index)">
                                    <img :src="item.goods_img" alt="">
                                </div>
                            </div>

                        </div>
                    </div>
                    <div class="blowUp" v-if="showBlowUp"><img class="blowUp-img" src="" alt=""></div>
                    <div class="goods-info" v-else>
                        <div class="info-name">{{ goodsDetail.goods_name }}</div>
                        <div class="info-introduction">{{ goodsDetail.goods_introduction }}</div>
                        <div class="info-size">
                            <div :class="{ sizeNum: true, sizeNumActive: selectedShoes.size === item.sku_size }"
                                v-for="item in skuList" :key="item.sku_id" @click="selectSizeAction(item)">{{
                                    item.sku_size }}</div>
                        </div>
                        <div class="info-price">
                            <span>￥</span><span>{{ selectedShoes.price || '--' }}</span>
                        </div>
                        <div class="info-inventory">当前库存：{{ selectedShoes.inventory }}</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<style scoped lang='scss'>
.container {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    position: relative;
    color: #333333;

    .temp {
        width: 100%;
        height: 80px;

        @media screen and (max-width:600px) {
            display: none;
        }
    }

    .main {
        margin-top: 20px;
        // width: 1300px;
        width: 90%;
        // display: flex;

        @media screen and (max-width:600px) {
            display: block;
        }

        .bread {
            width: 100%;
            background-color: white;
            box-sizing: border-box;
            padding: 15px 20px;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            border-radius: 10px;
            justify-content: space-between;

            .bread-link {
                margin-left: 10px;
                font-size: 13px;
                color: #ccc;
                display: flex;
                align-items: center;
            }
        }

        .goods {
            width: 100%;
            display: flex;
            position: relative;



            .goods-aside {
                width: 10%;
                margin-right: 20px;
                box-sizing: border-box;
                padding: 16px;
                background: white;
                border-radius: 10px;
            }

            .goods-photo {
                width: 30%;

                .photo-main {
                    width: 100%;
                    display: flex;
                    box-sizing: border-box;
                    padding: 16px;
                    background: white;
                    border-radius: 10px;


                    .main-large {
                        // width: 200px;
                        // height: 300px;
                        flex: 1;

                        img {
                            width: 100%;
                            height: 100%;
                        }
                    }

                    .main-list {
                        margin-left: 20px;
                        width: 10%;
                        display: flex;
                        flex-direction: column;
                        align-items: center;

                        .mainListItem {
                            width: 50px;
                            height: 50px;
                            margin-bottom: 10px;

                            img {
                                width: 100%;
                                height: 100%;
                            }
                        }

                        .mainListItemActive {
                            border: solid 2px green;
                        }
                    }
                }



                .photo-type {
                    margin-top: 10px;
                    width: 100%;
                    display: flex;
                    align-items: center;
                    box-sizing: border-box;
                    padding: 8px 16px;
                    background: white;
                    border-radius: 10px;

                    .photo-type-list {
                        display: flex;
                        align-items: center;
                        overflow: auto;

                        .photoTypeItem {
                            margin: 0 10px;
                            width: 50px;
                            height: 50px;

                            img {
                                width: 100%;
                                height: 100%;
                            }
                        }

                        .photoTypeItemActive {
                            border: solid 3px #8e8cd4;
                        }
                    }


                }
            }

            .blowUp {
                transition: all .5s ease;
                // position: absolute;
                width: 500px;
                height: 500px;
                background: white;
                margin-left: 20px;
                border-radius: 10px;
                overflow: hidden;
                position: relative;

                .blowUp-img {
                    position: absolute;
                    width: 200%;
                    height: 200%;
                }
            }

            .goods-info {
                transition: all .5s ease;
                flex: 1;
                background: white;
                margin-left: 20px;
                border-radius: 10px;
                box-sizing: border-box;
                padding: 24px;

                .info-name {
                    font-size: 25px;
                }

                .info-introduction {
                    // width: 100%;
                    margin: 20px 0;
                    background-color: #eee;
                    box-sizing: border-box;
                    padding: 10px 15px;
                    border-radius: 10px;
                    color: #999999;
                    line-height: 20px;
                }

                .info-size {
                    // width: 100%;
                    height: 40px;
                    display: flex;

                    .sizeNum {
                        font-size: 15px;
                        margin: 0 20px;
                        line-height: 40px;
                        cursor: pointer;
                    }

                    .sizeNumActive {
                        color: #8e8cd4;
                        border-bottom: solid 1px #8e8cd4;
                    }
                }

                .info-price {
                    margin-top: 20px;

                    span:nth-child(2) {
                        font-size: 20px;
                    }
                }

                .info-inventory {
                    margin-top: 20px;
                }
            }
        }
    }


}
</style>